# -*- coding:utf-8 -*-

from pydantic import BaseModel, ConfigDict
from typing import Optional
from datetime import datetime


class AgentClientModel(BaseModel):
    """Agent客户端数据模型"""
    model_config = ConfigDict(from_attributes=True)
    
    id: Optional[int] = None
    client_name: Optional[str] = None
    client_id: Optional[str] = None
    ip_address: Optional[str] = None
    port: Optional[int] = None
    hostname: Optional[str] = None
    server_host_name: Optional[str] = None  # 服务器管理中的主机名称（如agent-1, agent-2）
    client_type: Optional[str] = None
    online_status: Optional[str] = None
    work_status: Optional[str] = None
    browser_status: Optional[str] = None
    login_status: Optional[str] = None
    account: Optional[str] = None
    account_nickname: Optional[str] = None
    cookies: Optional[str] = None  # Cookie数据（JSON字符串）
    default_address_id: Optional[str] = None
    default_address_info: Optional[str] = None  # 默认地址详细信息（JSON字符串）
    connected_at: Optional[datetime] = None
    last_heartbeat: Optional[datetime] = None
    last_error: Optional[str] = None
    error_count: Optional[int] = None
    remark: Optional[str] = None
    assigned_user_id: Optional[int] = None  # ✅ 分配的用户ID（单个用户，1对1分配）
    assigned_user_name: Optional[str] = None  # ✅ 分配的用户名（用于前端显示）
    status: Optional[str] = None
    create_time: Optional[datetime] = None
    update_time: Optional[datetime] = None


class AgentClientQueryModel(BaseModel):
    """Agent客户端查询参数模型"""
    client_name: Optional[str] = None
    client_id: Optional[str] = None
    client_type: Optional[str] = None
    online_status: Optional[str] = None
    login_status: Optional[str] = None
    status: Optional[str] = None


class AgentClientCreateModel(BaseModel):
    """Agent客户端创建模型"""
    client_name: str
    client_type: Optional[str] = 'search'
    remark: Optional[str] = None


class AgentClientUpdateModel(BaseModel):
    """Agent客户端更新模型"""
    client_name: Optional[str] = None
    client_type: Optional[str] = None
    remark: Optional[str] = None
    default_address_id: Optional[str] = None
    default_address_info: Optional[str] = None  # 默认地址详细信息（JSON字符串）
    status: Optional[str] = None


class AgentClientCommandModel(BaseModel):
    """Agent客户端命令模型"""
    command: str  # start/stop/login/search/order
    data: Optional[dict] = None


class AgentAssignModel(BaseModel):
    """Agent分配模型（1对1分配）"""
    client_ids: list[int]  # 要分配的agent ID列表
    user_id: Optional[int] = None  # ✅ 分配的用户ID（单个用户），None表示取消分配（回归管理员）

